<?php namespace App\Http\Services;

use Requests;
use Requests_Exception;

class YunPian
{
    private $username = 'gzzrhy';
    private $password = 'Zr2020527';
    private $key = 'ssd778354';
    private $baseUrl = 'http://hy.0571smsyun.com:8086';
    private $baseUrls = 'https://hy.0571smsyun.com:8443';
    private $sendSmsUrl = '/getsms';
    private $sendTplSmsUrl = '/batchSubmit';
    private $getStatus = '/SmsStatus/getStatus';
    public function sendSms($sendTo, $content)
    {
        $result = self::doPost($this->sendSmsUrl, [
            'username' => $this->username,
            'password' => strtoupper(MD5($this->password)),
            'mobile' => $sendTo,
            'content'   => $content,
        ]);
        return $result;
    }

    public function sendBasicSmsWithCode($sendTo, $code)
    {
        $result = self::doPost($this->sendSmsUrl, [
            'username' => $this->username,
            'password' => strtoupper(MD5($this->password)),
            'mobile'   => $sendTo,
//            'content'  => '【省人协】张寅虎的vip快到期了，如有需要，请尽快联系，商议续费事宜！',
            'content'  => '【省人协】您的验证码是' . $code,
//            'extend'   => 10000,
//            'sign '    => strtoupper(MD5($this->username . $sendTo . 10000 . $this->key))
        ]);

//        if(!empty($result) && strpos($result,'result=0') !== false){
//            $msgIdAddress = strpos($result,'msgId');
//            if($msgIdAddress != false){
//                $msgId = substr($result,($msgIdAddress + 6));
//
//
//            }
//        }

        return $result;
    }

    private function doPost($url, array $param)
    {
        $response = null;
        try {
            $response = Requests::post($this->baseUrls . $url, array(), $param);
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To YunPian:'.$ex);
        }

        if (!$response->success) {
            return false;
        }

        if (empty($response->body))
            return false;

        return $response->body;
    }
}
